{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 设置 matplotlib 使用支持中文的字体\n",
    "# 'Microsoft YaHei' 是 Windows 系统中常见的字体\n",
    "# 如果您使用 macOS，可以尝试 'PingFang SC'\n",
    "# 如果您使用 Linux，可以尝试 'WenQuanYi Micro Hei'\n",
    "plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] \n",
    "\n",
    "# 解决负号 '-' 显示为方块的问题\n",
    "plt.rcParams['axes.unicode_minus'] = False \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Jupyter笔记本中的可维护性问题\n",
    "\n",
    "这个笔记本演示了Jupyter在版本控制和维护方面的问题：\n",
    "1. JSON格式难以进行版本控制\n",
    "2. 输出和元数据污染版本历史\n",
    "3. 合并冲突难以解决"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 问题1：JSON格式的版本控制困难\n",
    "\n",
    "Jupyter笔记本以JSON格式存储，包含大量元数据："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 简单的计算\n",
    "a = 6\n",
    "b = 7\n",
    "result = a * b\n",
    "print(\"这是一个简单的计算\")\n",
    "print(f\"结果: {result}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "# 创建一个简单的图表\n",
    "x = np.linspace(1, 10, 10)\n",
    "y = x ** 2\n",
    "\n",
    "plt.bar(x, y)\n",
    "plt.title(\"简单的柱状图\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**问题说明**：\n",
    "\n",
    "上面这个简单的笔记本在JSON中会包含：\n",
    "- 大量的元数据（cell_type, metadata, execution_count等）\n",
    "- 完整的图像数据（base64编码的PNG）\n",
    "- 输出文本的完整副本\n",
    "- 内核信息和其他配置\n",
    "\n",
    "这导致：\n",
    "1. **文件很大**：即使是简单的代码，JSON文件也可能很大\n",
    "2. **diff困难**：版本控制工具很难显示有意义的差异\n",
    "3. **合并冲突**：多人协作时经常出现难以解决的合并冲突\n",
    "4. **输出污染**：每次运行都会改变输出，导致不必要的版本变更"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 问题2：输出和元数据的版本控制问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "import random\n",
    "\n",
    "# 每次运行都会产生不同的输出\n",
    "print(f\"当前时间: {datetime.now()}\")\n",
    "print(f\"随机数: {random.random()}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**问题**：\n",
    "- 每次运行这个单元格，输出都会改变\n",
    "- 即使代码没有变化，笔记本文件也会被标记为已修改\n",
    "- 版本历史中充满了无意义的输出变更\n",
    "- 难以区分真正的代码变更和输出变更"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 问题3：合并冲突示例\n",
    "\n",
    "当两个开发者同时修改同一个笔记本时，JSON结构使得合并变得困难："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 这是一个可能引起合并冲突的单元格\n",
    "def process_data(data):\n",
    "    # 开发者A可能会修改这里的逻辑\n",
    "    result = data * 2\n",
    "    return result\n",
    "\n",
    "# 开发者B也可能会修改这里的逻辑\n",
    "test_data = [1, 2, 3, 4, 5]\n",
    "processed = [process_data(x) for x in test_data]\n",
    "\n",
    "print(\"这是一个可能引起合并冲突的单元格\")\n",
    "print(\"开发者A可能会修改这里的逻辑\")\n",
    "print(\"开发者B也可能会修改这里的逻辑\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**典型的JSON合并冲突**：\n",
    "\n",
    "```json\n",
    "<<<<<<< HEAD\n",
    "   \"execution_count\": 4,\n",
    "   \"outputs\": [\n",
    "    {\n",
    "     \"name\": \"stdout\",\n",
    "     \"output_type\": \"stream\",\n",
    "     \"text\": [\n",
    "      \"开发者A的输出\\n\"\n",
    "     ]\n",
    "    }\n",
    "   ],\n",
    "=======\n",
    "   \"execution_count\": 7,\n",
    "   \"outputs\": [\n",
    "    {\n",
    "     \"name\": \"stdout\",\n",
    "     \"output_type\": \"stream\",\n",
    "     \"text\": [\n",
    "      \"开发者B的输出\\n\"\n",
    "     ]\n",
    "    }\n",
    "   ],\n",
    ">>>>>>> feature-branch\n",
    "```\n",
    "\n",
    "这种冲突很难手动解决，特别是当涉及复杂的输出（如图表）时。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "marimo-ui (3.12.11)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
